package com.peas.platform.module.icims.expenditure.setting.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.expenditure.setting.domain.ExpenditureSettingsDomain;
import com.peas.platform.module.icims.expenditure.setting.dto.ExpenditureSettingsDto;
import com.peas.platform.module.icims.expenditure.setting.enums.ExpenditureSettingsType;
import com.peas.platform.module.icims.expenditure.setting.service.ExpenditureSettingsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ExpenditureSettingsServiceImpl extends BaseServiceImpl<ExpenditureSettingsDto> implements ExpenditureSettingsService {

    @Resource
    private ExpenditureSettingsDomain expenditureSettingsDomain;

    @Override
    public BaseDomain<ExpenditureSettingsDto, ? extends BasePo> getDomain() {
        return expenditureSettingsDomain;
    }

    @Override
    public List<ExpenditureSettingsDto> getExpenditureSettingsDto(ExpenditureSettingsType... expenditureSettingsTypes) {
        Assert.notNull(expenditureSettingsTypes, "配置类型不能为空");
        List<String> types = new ArrayList<>();
        for (ExpenditureSettingsType type : expenditureSettingsTypes) {
            types.add(type.getKey());
        }
        Assert.notEmpty(types, "配置类型不能为空");
        ExpenditureSettingsDto c = new ExpenditureSettingsDto();
        c.setSettingsType(CollUtil.join(types, StrPool.COMMA));
        List<ExpenditureSettingsDto> list = this.list(c);
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<ExpenditureSettingsDto> result = new ArrayList<>();
        list.forEach(dto -> {
            if (types.contains(dto.getSettingsType()) && !result.contains(dto)) {
                result.add(dto);
            }
        });
        return result;
    }

}
